import pickle
from athletelist import AthleteList

def get_coach_data(filename):
	try:
		with open(filename) as f:
			data = f.readline()
			templ = data.strip().split(',')
			return(AthleteList(templ.pop(0),templ.pop(0),templ))
	except IOError as err:
			print("File error: " + str(err))
			return(None)

# files_list 表示一个文件名列表
def put_to_store(files_list):		
	all_athletes = {}
	for filename in files_list:
		athlete = get_coach_data(filename)
		all_athletes[athlete.name] = athlete
	try:
		with open('athletes.pickle','wb') as athf:
			pickle.dump(all_athletes,athf)
	except IOError as err:
		print("File error (put_to_store) " + str(err))
	return(all_athletes)
	
def get_from_store():
	all_athletes = {}
	try:
		with open('athletes.pickle','rb') as athf:
			all_athletes = pickle.load(athf)
	except IOError as err:
		print("File error (get_from_store) " + str(err))
	return(all_athletes)
	
